/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Models.Admin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Cyro
 */
public class AdminDAO {

    public int addAdmin(Admin ad) {
        int result = 0;
        try {
            Connection con = (Connection) DBConnection.myConnection();
            PreparedStatement stm = con.prepareStatement("Insert into Admin values(?,?,?,?,?,?)");
            stm.setString(1, ad.getUsername());
            stm.setString(2, ad.getPassword());
            stm.setString(3, ad.getFullname());
            stm.setString(4, ad.getAddress());
            stm.setString(5, ad.getPhone());
            stm.setInt(6, 2);
            result = stm.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    public Admin getUser(Admin admin) {
        Admin a = new Admin();
        try {
            Connection con = (Connection) DBConnection.myConnection();
            PreparedStatement stmt = con.prepareStatement("select * from Admin where Username = '" + admin.getUsername() + "'");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                a = new Admin(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getInt(7));
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a;
    }

    public Admin getUserByID(int id) {
        Admin a = new Admin();
        try {
            Connection con = (Connection) DBConnection.myConnection();
            PreparedStatement stmt = con.prepareStatement("select * from Admin where ID=" + id);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                a = new Admin(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getInt(7));
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a;
    }

    public ArrayList<Admin> getAllAdmin() {
        ArrayList<Admin> listAdmin = new ArrayList<Admin>();
        try {
            Connection con = (Connection) DBConnection.myConnection();
            PreparedStatement stm = con.prepareStatement("select * from Admin");
            ResultSet rs = stm.executeQuery();

            while (rs.next()) {
                Admin ad = new Admin();
                ad.setId(rs.getInt("ID"));
                ad.setUsername(rs.getString("Username"));
                ad.setPassword(rs.getString("Password"));
                ad.setFullname(rs.getString("Fullname"));
                ad.setAddress(rs.getString("Address"));
                ad.setPhone(rs.getString("Phone"));
                ad.setRole(rs.getInt("Role"));

                listAdmin.add(ad);
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listAdmin;
    }

    public void deleteAdminByID(int id) {
        try {
            Connection con = (Connection) DBConnection.myConnection();
            PreparedStatement stm = con.prepareStatement("DELETE FROM Admin WHERE ID = ?");
            stm.setInt(1, id);
            stm.executeUpdate();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void editAdmin(int id, String username, String password, String fullname, String address, String phone) throws Exception {
        Connection con = (Connection) DBConnection.myConnection();
        String sql = "update Admin set Username=?, Password=?, Fullname=?, [Address]=?, Phone=? where ID=?";
        PreparedStatement st = con.prepareStatement(sql);
        st.setString(1, username);
        st.setString(2, password);
        st.setString(3, fullname);
        st.setString(4, address);
        st.setString(5, phone);
        st.setInt(6, id);
        st.executeUpdate();
        con.close();
    }
}
